package com.cn.demo4.service.impl;

import com.cn.demo4.dao.NewsDao;
import com.cn.demo4.dao.impl.NewsDaoImpl;
import com.cn.demo4.model.NavModel;
import com.cn.demo4.model.NewsModel;
import com.cn.demo4.service.INewsService;
import com.cn.demo4.util.Tool;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class NewsServiceImpl implements INewsService {
    /**
     * 查询所有新闻信息
     *
     * @return
     */
    @Override
    public List<NewsModel> findAll(NewsModel newsModel) {
        NewsDao newsDao = new NewsDaoImpl();
        return newsDao.findAll(newsModel);
    }

    /**
     * 插入数据
     *
     * @param newsModel
     * @return
     */
    @Override
    public String add(NewsModel newsModel) {
        NewsDao newsDao = new NewsDaoImpl();
        int result = newsDao.add(newsModel);
        if(result == 1) {
            Jedis jedis = new Jedis();
            jedis.flushDB();
        }
        //返回的是数据库影响条数故是int类型,当又数据更新是给一个标识符：success，反之亦然
        return result == 1 ? "success" : "error";
    }

    @Override
    public NewsModel findById(Integer id) {
        NewsDao newsDao = new NewsDaoImpl();
        return newsDao.findById(id);
    }

    @Override
    public String update(NewsModel newsModel) {
        NewsDao newsDao = new NewsDaoImpl();
        int result = newsDao.update(newsModel);
        if(result == 1) {
            Jedis jedis = new Jedis();
            jedis.flushDB();
        }
        return result == 1 ? "success" : "error";
    }

    @Override
    public int del(Integer id) {
        NewsDao newsDao = new NewsDaoImpl();
        int result = newsDao.del(id);
        if(result == 1) {
            Jedis jedis = new Jedis();
            jedis.flushDB();
        }
        return result;
    }

    @Override
    public List<NewsModel> getNewsModelListForHome() {
        List<NewsModel> list = new ArrayList<>();
        Jedis jedis = new Jedis();
        NewsDao newsDao = new NewsDaoImpl();
        Boolean exists = jedis.exists("newsId");
        if (exists) {
            List<String> newsIdList = jedis.lrange("newsId", 0, -1);
            for (int i = 0; i < newsIdList.size(); i++) {
                NewsModel newsModel = new NewsModel();
                newsModel.setId(Tool.stringToInt(jedis.lindex("newsId", i)));
                newsModel.setImg(jedis.lindex("newsImg", i));
                newsModel.setTitle(jedis.lindex("newsTitle", i));
                newsModel.setTypeName(jedis.lindex("newsTypeName", i));
                newsModel.setPublisher(jedis.lindex("newsPublisher", i));
                newsModel.setCreateTime(jedis.lindex("newsCreateTime", i));
                newsModel.setUpdateTime(jedis.lindex("newsUpdateTime", i));
                newsModel.setStatus(Tool.stringToInt(jedis.lindex("newsStatus", i)));
                newsModel.setContent(jedis.lindex("newsContent", i));
                list.add(newsModel);
            }
        } else {
            //查询到数据保存到redis中
            list = newsDao.getNewsModelListForHome();
            for(int i = 0 ; i<list.size();i++){
                jedis.rpush("newsId",Integer.toString(list.get(i).getId()));
                jedis.rpush("newsStatus",Integer.toString(list.get(i).getStatus()));
                jedis.rpush("newsTitle",list.get(i).getTitle());
                jedis.rpush("newsTypeName",list.get(i).getTypeName());
                jedis.rpush("newsPublisher",list.get(i).getPublisher());
                jedis.rpush("newsCreateTime",list.get(i).getCreateTime());
                jedis.rpush("newsUpdateTime",list.get(i).getUpdateTime());
                jedis.rpush("newsContent",list.get(i).getContent());
                jedis.rpush("newsImg",list.get(i).getImg());
        }
    }
        return list;
}

    @Override
    public int getCount(NewsModel newsModel) {
        NewsDao newsDao = new NewsDaoImpl();
        return newsDao.geCount(newsModel);
    }

}
